//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "TrinomialTree.h"
using namespace Cephei::QL::Methods::Lattices;
#include <gen/QL/TimeGrid.h>
#include <gen/QL/StochasticProcess1D.h>
using namespace Cephei::QL;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (Cephei::QL::IStochasticProcess1D^ process, Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isPositive) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CStochasticProcess1D^ _Cprocess;
    CTimeGrid^ _CtimeGrid;
    try
    {
#ifdef HANDLE
        _phTrinomialTree = NULL;
#endif
        _Cprocess = safe_cast<CStochasticProcess1D^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::StochasticProcess1D>& _process = static_cast<boost::shared_ptr<QuantLib::StochasticProcess1D>&> (_Cprocess->GetShared ()); 
        _CtimeGrid = safe_cast<CTimeGrid^> (timeGrid);
        _CtimeGrid->Lock();
        QuantLib::TimeGrid& _timeGrid = static_cast<QuantLib::TimeGrid&> (_CtimeGrid->GetReference ()); 
        bool _isPositive = 
            (Microsoft::FSharp::Core::FSharpOption<Boolean>::IsSome::get (isPositive) ? (bool)ValueHelper::Convert (isPositive->Value) : false); //4
        _ppTrinomialTree = new boost::shared_ptr<QuantLib::TrinomialTree> (new QuantLib::TrinomialTree ( _process,  _timeGrid,  _isPositive ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
        if (_CtimeGrid != nullptr) _CtimeGrid->Unlock();
    }
}
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (boost::shared_ptr<QuantLib::TrinomialTree>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrinomialTree = NULL;
#endif
	_ppTrinomialTree = &childNative;
    
}
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (QuantLib::TrinomialTree& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phTrinomialTree = NULL;
#endif
	_ppTrinomialTree = new boost::shared_ptr<QuantLib::TrinomialTree> (&childNative);
    
    _TrinomialTreeOwner = owner;
    
}

Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (CTrinomialTree^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrinomialTree = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppTrinomialTree = new boost::shared_ptr<QuantLib::TrinomialTree> (copy->GetShared());
        
    }
}
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrinomialTree = NULL;
#endif
	if (!t->IsSubclassOf(CTrinomialTree::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (QuantLib::Handle<QuantLib::TrinomialTree>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTrinomialTree = &childNative;
	_ppTrinomialTree = &static_cast<boost::shared_ptr<QuantLib::TrinomialTree>>(childNative.currentLink());
    
    _TrinomialTreeOwner = owner;
}
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (QuantLib::Handle<QuantLib::TrinomialTree> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phTrinomialTree = &childNative;
	_ppTrinomialTree = &static_cast<boost::shared_ptr<QuantLib::TrinomialTree>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Methods::Lattices::CTrinomialTree::CTrinomialTree (QuantLib::TrinomialTree childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phTrinomialTree = NULL;
#endif
	_ppTrinomialTree = new boost::shared_ptr<QuantLib::TrinomialTree> (new QuantLib::TrinomialTree (childNative));
    
}
#endif

Cephei::QL::Methods::Lattices::CTrinomialTree::~CTrinomialTree ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppTrinomialTree != NULL)
    {
	    delete _ppTrinomialTree;
        _ppTrinomialTree = NULL;
    }
}
Cephei::QL::Methods::Lattices::CTrinomialTree::!CTrinomialTree ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppTrinomialTree != NULL)
    {
	    delete _ppTrinomialTree;
    }
}
QuantLib::TrinomialTree& Cephei::QL::Methods::Lattices::CTrinomialTree::GetReference ()
{
    if (_ppTrinomialTree == NULL) throw REFNEW NativeNullException ();
	return **_ppTrinomialTree;
}
boost::shared_ptr<QuantLib::TrinomialTree>& Cephei::QL::Methods::Lattices::CTrinomialTree::GetShared ()
{
    if (_ppTrinomialTree == NULL) throw REFNEW NativeNullException ();
	return *_ppTrinomialTree;
}
QuantLib::TrinomialTree* Cephei::QL::Methods::Lattices::CTrinomialTree::GetPointer ()
{
    if (_ppTrinomialTree == NULL) throw REFNEW NativeNullException ();
	return &**_ppTrinomialTree;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::TrinomialTree>& Cephei::QL::Methods::Lattices::CTrinomialTree::GetHandle ()
{
	if (_phTrinomialTree == NULL)
	{
		_phTrinomialTree = new Handle<QuantLib::TrinomialTree> (*_ppTrinomialTree);
	}
	return *_phTrinomialTree;
}
#endif
bool Cephei::QL::Methods::Lattices::CTrinomialTree::HasNative () 
{
	return (_ppTrinomialTree != NULL);
}

UInt64 Cephei::QL::Methods::Lattices::CTrinomialTree::Descendant (UInt64 i, UInt64 index, UInt64 branch)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index); //a
        QuantLib::Size _branch = (QuantLib::Size)ValueHelper::Convert (branch); //a
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTrinomialTree)->descendant ( _i,  _index,  _branch );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Lattices::CTrinomialTree::Dx (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTrinomialTree)->dx ( _i );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Lattices::CTrinomialTree::Probability (UInt64 i, UInt64 index, UInt64 branch)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index); //a
        QuantLib::Size _branch = (QuantLib::Size)ValueHelper::Convert (branch); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTrinomialTree)->probability ( _i,  _index,  _branch );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Methods::Lattices::CTrinomialTree::Size (UInt64 i)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppTrinomialTree)->size ( _i );   
        UInt64 _nrv = _rv; //c1
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::ITimeGrid^ Cephei::QL::Methods::Lattices::CTrinomialTree::TimeGrid::get ()
{
    try
    {
    	QuantLib::TimeGrid& _rv = (QuantLib::TimeGrid&)(*_ppTrinomialTree)->timeGrid ( );   
        Cephei::QL::CTimeGrid^ _nrv = REFNEW Cephei::QL::CTimeGrid (_rv, this);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Methods::Lattices::CTrinomialTree::Underlying (UInt64 i, UInt64 index)
{
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i); //a
        QuantLib::Size _index = (QuantLib::Size)ValueHelper::Convert (index); //a
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppTrinomialTree)->underlying ( _i,  _index );   
        Double _nrv = (Double)ValueHelper::Convert (_rv); //c
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Methods::Lattices::ITrinomialTree^ Cephei::QL::Methods::Lattices::CTrinomialTree_Factory::Create (Cephei::QL::IStochasticProcess1D^ process, Cephei::QL::ITimeGrid^ timeGrid, Microsoft::FSharp::Core::FSharpOption<Boolean>^ isPositive)
{
    return REFNEW CTrinomialTree ( process,  timeGrid,  isPositive);
}
